package com.ma4567.remotecontroller.InfoRequest;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.ma4567.remotecontroller.MysqlHelper.TableLogManager;
import com.ma4567.remotecontroller.MysqlHelper.TableRequestLog;
import com.ma4567.remotecontroller.RemoteControllerApplication;
import com.ma4567.remotecontroller.ffmpegManager.ffmpegController;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.*;
import oshi.*;
import oshi.hardware.*;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Properties;

class getSystemInfoResponse extends com.ma4567.remotecontroller.util.CustomRespone{
    public String OsName = "null";
    public String OsVersion = "null";
    public String OsArch = "null";

    public ArrayList<String> GraphicCard = new ArrayList<>();
    public ArrayList<String> Ram = new ArrayList<>();
    public String Cpu;
    public String CpuFactory;
    public String CpuCore;
    public ArrayList<String> Disk = new ArrayList<>();
    public String MotherBoard;

    public getSystemInfoResponse(int Status) {
        super(Status);

        if(Status == 0){
            SystemInfo systemInfo = new SystemInfo();
            HardwareAbstractionLayer hardware = systemInfo.getHardware();

            Properties props = System.getProperties();
            this.OsName = props.getProperty("os.name");
            this.OsVersion = props.getProperty("os.version");
            this.OsArch = props.getProperty("os.arch");

            for(GraphicsCard gc : hardware.getGraphicsCards()){
                this.GraphicCard.add(gc.getName());
            }

            for(PhysicalMemory mem : hardware.getMemory().getPhysicalMemory()){
                this.Ram.add(mem.getManufacturer() + " " + (mem.getCapacity() / (1024 * 1024 * 1024)) + "GB " + (mem.getClockSpeed() / 1000000) + "MT/s");
            }

            this.Cpu = hardware.getProcessor().getProcessorIdentifier().getName();
            if(this.Cpu.contains("AMD")) this.CpuFactory = "AMD";
            else if(this.Cpu.contains("Intel")) this.CpuFactory = "Intel";
            else this.CpuFactory = hardware.getProcessor().getProcessorIdentifier().getVendor();

            this.CpuCore = hardware.getProcessor().getPhysicalProcessorCount() + "核 " + hardware.getProcessor().getLogicalProcessorCount() + "线程";

            for(HWDiskStore disk: hardware.getDiskStores()){
                this.Disk.add(disk.getModel() + " " + (disk.getSize() / (1024 * 1024 * 1024)) + "GB");
            }

            ComputerSystem computerSystem = hardware.getComputerSystem();
            Firmware firmware = computerSystem.getFirmware();
            this.MotherBoard = firmware.getManufacturer() + " " + firmware.getName();
        }
    }
}

@CrossOrigin
@RestController
public class GetSystemInfo {
    @RequestMapping(value = "/api/getSystemInfo", method = RequestMethod.GET)
    public String getSystemInfo(HttpServletRequest request,
                                @RequestParam("cookie") String Cookie) throws SQLException {
        ffmpegController.StopStream();
        TableRequestLog.AddLog("Request system information", Cookie, request.getRemoteAddr());
        if(!RemoteControllerApplication.userManager.JudgeCookie(Cookie)){//鉴权
            return JSONObject.toJSONString(new getSystemInfoResponse(1), SerializerFeature.PrettyFormat);
        }
        else {
            TableLogManager.AddLog("Request system information", Cookie);
            return JSONObject.toJSONString(new getSystemInfoResponse(0), SerializerFeature.PrettyFormat);
        }
    }

    public static void main(String[] args) {
        SystemInfo systemInfo = new SystemInfo();
        HardwareAbstractionLayer hardware = systemInfo.getHardware();
        ComputerSystem computerSystem = hardware.getComputerSystem();
        Firmware firmware = computerSystem.getFirmware();

    }
}
